<?php

declare (strict_types=1);

namespace app\api\controller;


use app\api\service\User as UserService;
use app\common\library\helper;
use app\common\model\OrderSub as OrderSubModel;
use app\common\service\bangdou\Common as CommonService;
use think\response\Json;

class BdVipCard extends Controller
{
    //会员等级列表
    public function levelList(): Json
    {
        $url = app()->config->get('app.bd_json_url') . '/api/getVipLevelData';
        $result = helper::httpRequest($url, true);
        $result = json_decode($result, true);
        if ($result['code'] == 0) {
            return $this->renderSuccess($result['list']);
        } else {
            return $this->renderError($result['message']);
        }

    }

    //查看会员卡详情
    public function levelInfo()
    {
        $result = $this->request->param();
        $url = app()->config->get('app.bd_json_url') . '/api/getVipLevelDataInfo';
        $params['type'] = isset($result['type']) ? $result['type'] : 2;
        $result = helper::httpRequest($url, $params);
        $result = json_decode($result, true);
        if ($result['code'] == 0) {
            return $this->renderSuccess($result['data']);
        } else {
            return $this->renderError($result['message']);
        }
    }

    //支付验证
    /*public function actionLevelVerify()
    {
        $request = Yii::$app->request;
        if ($request->isGet) {
            return $this->result(2, '请求异常');
        }
        $result = axios_request();
        if (isset($result['type']) && in_array($result['type'], array('2', '3', '4'))) {
            $vipLevel = ['2' => ['type' => '2', 'vipName' => '黄金会员', 'vipPrice' => '198'],
                '3' => ['type' => '3', 'vipName' => '铂金会员', 'vipPrice' => '398'],
                '4' => ['type' => '4', 'vipName' => '钻石金会员', 'vipPrice' => '798']];
            if ($vipLevel[$result['type']]) {
                return $this->result(1, '成功', $vipLevel[$result['type']]);
            }
        }
        return $this->result(2, '参数错误');
    }*/

    //邦豆劵账户信息
    public function userBd()
    {
        if ($this->request->method() != 'POST') {
            return $this->renderError('请求异常');
        }
        $result = $this->request->param();
        $token_array = CommonService::createTokenByuserId();
        $data['timestamp'] = time();
        $data['platform'] = 'shopping';
        $sign = CommonService::getBangDouApiSign($data);
        $data['sign'] = $sign;
        $headers = [
            'Content-Type: application/json',
            "Authorization: Bearer {$token_array['token']}"
        ];
        if ($result['type'] == 7) {
            $api_url = app()->config->get('app.bd_api_url') . '/Bangdou/Manage/accountInfo';
        } elseif ($result['type'] == 6) {
            $api_url = app()->config->get('app.bd_api_url') . '/CashCoupon/Manage/accountInfo';
        } elseif ($result['type'] == 5) {
            $api_url = app()->config->get('app.bd_api_url') . '/CashGiftCoupon/Manage/accountInfo';
        } else {
            return $this->renderError("参数错误");
        }

        $result = helper::httpRequest($api_url, json_encode($data), $headers);
        $res = json_decode($result, true);
        if ($res['state'] == true) {
            return $this->renderSuccess($res['data']);
        }
        return $this->renderError($res['message']);
    }

    //三券收支流水
    public function expensesList()
    {
        if ($this->request->method() != 'POST') {
            return $this->renderError('请求异常');
        }
        $result = $this->request->param();
        $userInfo = UserService::getCurrentLoginUser(true);
        $param['user_id'] = $userInfo['user_id'];
        $result['month'] = isset($result['pay_time']) ? $result['pay_time'] : date("Y-m");
        $firstday = $result['month'] . '-' . '01 00:00:00';
        $betweenTime['start_time'] = $result['month'] ? strtotime($firstday) : strtotime(date('Y-m-01'));
        $betweenTime['end_time'] = $result['month'] ? strtotime("$firstday +1 month") : strtotime(date('Y-m-30 23:59:59', time() - 3600 * 24));
        $param['betweenTime'] =$betweenTime;
        $param['bd_type'] = isset($result['bd_type']) ? $result['bd_type'] : 7;
        $pageSize = isset($result['pageSize']) ? $result['pageSize'] : 10;
        $list = (new OrderSubModel)->getList($param,$pageSize)->toArray();
        if ($list['data']) {
            $data = [];
            foreach ($list['data'] as $key => $value) {
                $data[$key]['pay_amount'] = number_format($value['deduction_value'] + $value['pay_money'], 2);
                $data[$key]['pay_bd'] = $value['deduction_value'];
                $data[$key]['outTradeNo'] = $value['order_no'];
                $data[$key]['pay_money'] = $value['pay_money'];
                $data[$key]['order_id'] = $value['order_id'];
                $data[$key]['pay_time'] = date('Y-m-d H:i', $value['pay_time']);
            }
            $list['data'] = $data;
        }
        return $this->renderSuccess($list);
    }

    public function cardRule()
    {
        if ($this->request->method() != 'POST') {
            return $this->renderError('请求异常');
        }
        $result = $this->request->param();
        $url = app()->config->get('app.bd_json_url') . '/api/getBangDouUseInfo';
        $type = isset($result['type']) ? $result['type'] : 1;
        if ($type == 6){
            $params['type'] = 2;
        }elseif ($type == 5){
            $params['type'] = 3;
        }else{
            $params['type'] = 1;
        }
        $result = helper::httpRequest($url, $params);
        $result = json_decode($result, true);
        if ($result['code'] == 0) {
            return $this->renderSuccess($result['data']);
        } else {
            return $this->renderError($result['message']);
        }
    }

}